@echo off
REM Flowable 非 Docker 安装脚本 (Windows)

echo ==========================================
echo Flowable 非 Docker 安装脚本
echo ==========================================
echo.

REM 检查 Java
echo 检查 Java 环境...
java -version >nul 2>&1
if errorlevel 1 (
    echo ❌ Java 未安装，请先安装 JDK 8+
    echo    下载地址: https://www.oracle.com/java/technologies/downloads/
    exit /b 1
)

echo ✅ Java 已安装
java -version

REM 检查 MySQL
echo.
echo 检查 MySQL 连接...
mysql --version >nul 2>&1
if errorlevel 1 (
    echo ⚠️  MySQL 客户端未安装，请确保 MySQL 服务已运行
) else (
    echo ✅ MySQL 客户端已安装
)

REM 创建目录
echo.
echo 创建必要的目录...
if not exist "flowable\downloads" mkdir flowable\downloads
if not exist "flowable\install" mkdir flowable\install
if not exist "flowable\logs" mkdir flowable\logs
if not exist "flowable\config" mkdir flowable\config
echo ✅ 目录创建完成

REM 下载 Flowable
set FLOWABLE_VERSION=6.8.0
set FLOWABLE_ZIP=flowable-%FLOWABLE_VERSION%.zip
set FLOWABLE_URL=https://github.com/flowable/flowable-engine/releases/download/flowable-%FLOWABLE_VERSION%/%FLOWABLE_ZIP%

if not exist "flowable\downloads\%FLOWABLE_ZIP%" (
    echo.
    echo 下载 Flowable %FLOWABLE_VERSION%...
    echo 如果下载失败，请手动下载并解压到 flowable\install 目录
    echo 下载地址: %FLOWABLE_URL%
    
    REM 尝试使用 PowerShell 下载
    powershell -Command "Invoke-WebRequest -Uri '%FLOWABLE_URL%' -OutFile 'flowable\downloads\%FLOWABLE_ZIP%'" 2>nul
    if errorlevel 1 (
        echo ⚠️  自动下载失败，请手动下载
        echo    下载地址: %FLOWABLE_URL%
        echo    保存到: flowable\downloads\%FLOWABLE_ZIP%
    )
)

REM 解压 Flowable
if exist "flowable\downloads\%FLOWABLE_ZIP%" (
    if not exist "flowable\install\flowable-%FLOWABLE_VERSION%" (
        echo.
        echo 解压 Flowable...
        powershell -Command "Expand-Archive -Path 'flowable\downloads\%FLOWABLE_ZIP%' -DestinationPath 'flowable\install' -Force" 2>nul
        if errorlevel 1 (
            echo ⚠️  自动解压失败，请手动解压
            echo    解压 flowable\downloads\%FLOWABLE_ZIP% 到 flowable\install 目录
        ) else (
            echo ✅ 解压完成
        )
    )
)

REM 配置数据库
echo.
echo ==========================================
echo 数据库配置
echo ==========================================
echo.
echo 请确保 MySQL 服务已运行，并执行以下 SQL 创建数据库：
echo.
echo CREATE DATABASE flowable CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
echo CREATE USER 'flowable'@'localhost' IDENTIFIED BY 'flowable123';
echo GRANT ALL PRIVILEGES ON flowable.* TO 'flowable'@'localhost';
echo FLUSH PRIVILEGES;
echo.
set /p CONFIRM="数据库已配置完成？(y/n) "
if /i not "%CONFIRM%"=="y" (
    echo 请先配置数据库，然后重新运行此脚本
    exit /b 1
)

REM 创建配置文件
echo.
echo 创建配置文件...
(
echo # 数据库配置
echo spring.datasource.url=jdbc:mysql://localhost:3306/flowable?characterEncoding=UTF-8^&useSSL=false^&serverTimezone=Asia/Shanghai
echo spring.datasource.username=flowable
echo spring.datasource.password=flowable123
echo spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
echo.
echo # Flowable 配置
echo flowable.database-schema-update=true
echo flowable.async-executor-activate=true
echo.
echo # 服务器配置
echo server.port=8080
) > flowable\config\application.properties

echo ✅ 配置文件已创建: flowable\config\application.properties
echo    请根据需要修改数据库连接信息

REM 创建启动脚本
echo.
echo 创建启动脚本...

(
echo @echo off
echo REM Flowable 独立应用启动脚本
echo.
echo set FLOWABLE_DIR=flowable\install\flowable-6.8.0
echo set CONFIG_DIR=flowable\config
echo.
echo if not exist "%%FLOWABLE_DIR%%" ^(
echo     echo ❌ Flowable 未安装，请先运行 install_standalone.bat
echo     exit /b 1
echo ^)
echo.
echo echo 启动 Flowable UI...
echo cd /d "%%FLOWABLE_DIR%%\wars\flowable-ui"
echo start "Flowable UI" java -Xms512m -Xmx1024m -Dspring.config.location=../../../../config/application.properties -jar flowable-ui.war
echo echo Flowable UI 已启动
echo echo 访问地址: http://localhost:8080/flowable-ui
echo.
echo echo 启动 Flowable REST API...
echo cd /d "%%FLOWABLE_DIR%%\wars\flowable-rest"
echo start "Flowable REST" java -Xms512m -Xmx1024m -Dspring.config.location=../../../../config/application.properties -Dserver.port=8081 -jar flowable-rest.war
echo echo Flowable REST API 已启动
echo echo 访问地址: http://localhost:8081/flowable-rest
echo.
echo echo ==========================================
echo echo 服务启动完成
echo echo ==========================================
echo echo Flowable UI: http://localhost:8080/flowable-ui
echo echo 默认账号: admin / test
echo echo.
echo echo Flowable REST API: http://localhost:8081/flowable-rest
) > flowable\start_standalone.bat

echo ✅ 启动脚本已创建: flowable\start_standalone.bat

echo.
echo ==========================================
echo 安装完成
echo ==========================================
echo.
echo 下一步：
echo 1. 检查并修改 flowable\config\application.properties 中的数据库配置
echo 2. 运行启动脚本: flowable\start_standalone.bat
echo 3. 部署流程文件: cd flowable ^&^& python deploy_all.py
echo.

pause

